import { cloneDeep } from "lodash";
import * as TYPE from "../action-type";

const initialVal = {
  userInfo: {
    id: "",
    nickname: "",
    avatar: "",
    website: "",
    intro: "",
    email: "",
    articleLikeSet: [], // 点赞过的文章集合
    commentLikeSet: [], // 点赞过的评论集合
  },
};

export const UserReducer = (state = initialVal, action) => {
  state = cloneDeep(state);
  switch (action.type) {
    case "TOKEN_NOT_EXIST":
      //不修改初始化数据
      break;
    case TYPE.USER_GETUSERINFO:
      state.userInfo = action.userInfo;
      break;
    case TYPE.USER_ARTICLELKE:
      state.userInfo.articleLikeSet.includes(action.articleId)
        ? state.userInfo.articleLikeSet.splice(
            state.userInfo.articleLikeSet.indexOf(action.articleId),
            1
          )
        : state.userInfo.articleLikeSet.push(action.articleId);
      break;
    case TYPE.USER_COMMENTLIKE:
      state.userInfo.commentLikeSet.includes(action.commentId)
        ? state.userInfo.commentLikeSet.splice(
            state.userInfo.commentLikeSet.indexOf(action.commentId),
            1
          )
        : state.userInfo.commentLikeSet.push(action.commentId);
      break;
    case TYPE.USER_LOGOUT:
      //恢复默认数据
      state.userInfo = {
        id: "",
        nickname: "",
        avatar: "",
        website: "",
        intro: "",
        email: "",
        articleLikeSet: [], // 点赞过的文章集合
        commentLikeSet: [], // 点赞过的评论集合
      };
      break;
    default:
  }
  return state;
};
